/** 战国之刃,更新了所有人物的蓄力和连发优先级(避免与蓄力冲突)

  By setycyas @2025-06-17
*/

/* 全局设定
*/ {
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
#Include "..\Class\MyEmu.2.ahk"
#Include "..\Class\OrderMap.2.ahk"
;SendMode("Event")
SendMode("Input")
}

/* 制作战国之刃的宏类
*/ 
class TengaiMacro extends CommonMacro {

  pressDuration := 30
  aFlag := False ;a键按下与否的标记

  /* 函数
  */
  ;一键从蓄力到放开
  _一键蓄力(duration){
    if (this.aFlag) {
      this._up(1)
      this.aFlag := False
      sleep(20)
    }
    this._down(1)
    this.aFlag := True
    sleep(duration)
    this._up(1)
    this.aFlag := False
  }
  
  /* 新增的宏函数
  */
  _Ayin蓄力(key){
    this._一键蓄力(220)
  }
  _Junis蓄力(key){
    this._一键蓄力(1000)
  }
  _Katana蓄力(key){
    this._一键蓄力(1000)
  }
  _Miko蓄力(key){
    this._一键蓄力(700)
  }
  _Tengai蓄力(key){
    this._一键蓄力(700)
  }
  _Sho蓄力(key){
    this._一键蓄力(1000)
  }
  
  ;改装一下连发A,当被蓄力斩中断后判断一下按键的放开状态
  _连发A(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    while (MyFuns.getVkState(vk)) {
      this._down(1)
      this.aFlag := True
      sleep(this.pressDuration)
      if (this.aFlag) {
        this._up(1)
        this.aFlag := False
      }
      sleep(this.autoFireDelay)
    }
  }
  ;在按下攻击键蓄力时强制终止连射然后开始蓄力
  _手动蓄力(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    if (this.aFlag) {
      this._up(1)
      this.aFlag := False
    }
    this._down(1)
    while (MyFuns.getVkState(vk)) {
      sleep(20)
    }
    this._up(1)
  }

  /* 重载宏接口
  */
  _setMacro(){
    static obm := ObjBindMethod ;函数名简写,制作对象绑定函数
    ;制作宏相关Map
    this.macroMap := OrderMap()
    this.macroMap["连发A"] := obm(this, "_连发A")
    this.macroMap["Ayin蓄力"] := obm(this, "_Ayin蓄力")
    this.macroMap["Junis蓄力"] := obm(this, "_Junis蓄力")
    this.macroMap["Katana蓄力"] := obm(this, "_Katana蓄力")
    this.macroMap["Miko蓄力"] := obm(this, "_Miko蓄力")
    this.macroMap["Tengai蓄力"] := obm(this, "_Tengai蓄力")
    this.macroMap["Sho蓄力"] := obm(this, "_Sho蓄力")
    this.macroMap["手动蓄力"] := obm(this, "_手动蓄力")
    ;制作指定宏顺序的数组
    this.macroArr := this.macroMap.getKeys()
  }
}

/* 制作战国之刃专用UI
*/
class TengaiUI extends CommonUI {

  DEFAULT_HOTKEYS := "~d"
  EXE_ARRAY := ["mame.exe", "arcade64.exe", MyEmu.XZONE]

  /* 构造函数
  */
  __New(macro){
    super.__New("战国之刃", macro)
    this._preset := OrderMap([
      "Ayin", ["连发A", "Ayin蓄力", "手动蓄力"],
      "Junis", ["连发A", "Junis蓄力", "手动蓄力"],
      "Katana", ["连发A", "Katana蓄力", "手动蓄力"],
      "Miko", ["连发A", "Miko蓄力", "手动蓄力"],
      "Tengai", ["连发A", "Tengai蓄力", "手动蓄力"],
      "Sho", ["连发A", "Sho蓄力", "手动蓄力"],
    ])
    this.MACRO_PRIORITY["连发A"] := "p-1"
    this._macro.UI := this ;绑定UI
  }

  /* 重写接口
  */
  ;** 绑定热键,连发需要增加优先度
  /*
  _setHotkeys(){
    local keyName, macroName
    HotIfWinActive(this._winTitle)
    loop this._hotkeys.Length {
      keyName := this._hotkeys[A_Index]
      macroName := this._macroComboBoxes[A_Index].Text
      if ((keyName != "") and (this._macro.macroMap.Has(macroName))){
        if (macroName == "连发A") {
          Hotkey(keyName, this._macro.macroMap[macroName], "p-1")
        } else {
          Hotkey(keyName, this._macro.macroMap[macroName], "p0")
        }
      }
    }
    HotIfWinActive()
  }
  */

  /* 重写setup,更改可选的exe,读入预设
  */
  setup(){
    super.setup()
    this._myGui.opt("+AlwaysOnTop")
    this._myGui.show("x1100 y330")
    this.readPresetMenu("Ayin")
  }

}

/* 运行当前脚本
*/ 
if (A_ScriptName == "战国之刃.2.ahk") {
  face := FaceDetector() ;方向检测对象
  macro := TengaiMacro(face, ["q","f"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;新建界面对象并显示
  ui := TengaiUI(macro)
  ui.setup()
  ;更新组件内容
  ;ui._exeComboBox.Choose(MyEmu.XZONE)
  ui._exeComboBox.Choose(MyEmu.MAME)
  ui._gameKeyEdit.Text := "q,space"
  ui._hotkeyEdit.Text := "~s,~d,~a"
  ui._fpsEdit.Text := 10
  ui._testEdit.Text := "200,30,30,30"
  ui.readPresetMenu("Ayin")
}
